/**
 * Copyright 2011 Honeywell Federal Manufacturing & Technologies, LLC. This material was produced under U.S. Government
 * contract DE-ACO4-01AL66850, Honeywell Federal Manufacturing & Technologies, LLC, (FM&T) with the U. S. Department of
 * Energy for the operation of the Kansas City Plant . The U.S. Government has rights to use, reproduce, and distribute
 * this software.  NEITHER THE GOVERNMENT NOR Honeywell Federal Manufacturing & Technologies, LLC, MAKES ANY WARRANTY,
 * EXPRESS OR IMPLIED, OR ASSUMES ANY LIABILITY FOR THE USE OF THIS SOFTWARE.  If software is modified to produce
 * derivative works, such modified software should be clearly marked, so as not to confuse it with the version available
 * from FM&T.
 *
 * Additionally, this program is free software; you can redistribute it and/or modify it under the terms of the GNU
 * General Public License Version 3 published by the Free Software Foundation. Accordingly, this program is distributed in
 * the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with this library.
 * If not, see http://www.gnu.org/licenses/.
 */
package org.braintrain.domain.types;

import java.util.regex.Pattern;

/**
 * All the types of files accepted for upload to Media.  This enumeration
 * includes functions for producing the Regular Expression for checking a
 * filename for the given extensions.
 * 
 * @author John Ericksen
 *
 */

public enum MediaFileType {
    xls("xlsFile.gif"),
    doc("docFile.gif"),
    bmp("bmpFile.gif"),
    avi("aviFile.gif"),
    gif("gifFile.gif"),
    jpeg("jpgFile.gif"),
    jpg("jpgFile.gif"),
    mp3("mp3File.gif"),
    mpg("mpgFile.gif"),
    mpeg("mpgFile.gif"),
    pdf("pdfFile.gif"),
    png("pngfile.gif"),
    ram("movieFile.gif"),
    swf("swfFile.gif"),
    flv("swfFile.gif"),
    txt("txtFile.gif"),
    wav("wavFile.gif"),
    wma("wmaFile.gif"),
    wmv("wmvFile.gif");
    
    private static Pattern types = null;
    private String iconFile = "media.gif";

    private MediaFileType(){
        //empty
    }

    private MediaFileType(String iconFile){
        this.iconFile = iconFile;
    }

    /**
     * Builds the Pattern used to determine if the file extension is valid.
     *
     */
    private static void generateTypes()
    {
        StringBuffer buff = new StringBuffer();
        
        buff.append(".+\\.(");
        
        boolean first = true;
        for(MediaFileType type: values())
        {
            if(first)
            {
                first = false;
            }
            else
            {
                buff.append("|");
            }
            buff.append("(" + type.name() + ")");
        }
        
        buff.append(")");
        
        types = Pattern.compile(buff.toString());
    }
    
    /**
     * Returns the Regular expression for determining if the file extension is valid.
     * 
     * @return
     */
    public static Pattern getTypes() {
        if(types == null)
        {
            generateTypes();
        }
        return types;
    }
    
    /**
     * Returns the string of all valid file types.
     * @return
     */
    public static String getTypesString()
    {
        StringBuffer buff = new StringBuffer();
        
        boolean first = true;
        for(MediaFileType type: values())
        {
            if(first)
            {
                first = false;
            }
            else
            {
                buff.append(", ");
            }
            buff.append(type);
        }
        
        return buff.toString();
    }

    /**
     * returns which FileType the given filename fits into.
     * 
     * @param name
     * @return
     */
    public static MediaFileType getFileType(String name) {

        String lowercase = name.toLowerCase();
        for(MediaFileType type: values())
        {
            if(lowercase.endsWith("." + type.name()))
            {
                return type;
            }
        }
        
        return null;
    }

    public String getIconFile() {
        return iconFile;
    }
}
